<template>
  <div>
    <el-container style="height: 800px; border: 1px solid #eee">
      <el-aside  width="200px" style="background-color: rgb(238, 241, 246)">
        <Aside></Aside>
      </el-aside>
      <el-container>
        <el-header style="text-align: right; font-size: 12px;">
          <Head></Head>
        </el-header>
        <el-main>
          <el-table
              :data="tableData"
              style="width: 100%">
            <el-table-column
                prop="classId"
                label="班级ID"
                >
            </el-table-column>
            <el-table-column
                prop="className"
                label="班级名称"
                >
            </el-table-column>
            <el-table-column
                prop="classLocal"
                label="班级地点"
                >
            </el-table-column>
            <el-table-column
                fixed="right"
                label="操作"
                width="100px">
              <template #default="scope">
                <el-button @click="showClassInfo(scope.row)" type="text" size="small">查看班级信息</el-button>
<!--                <el-button type="text" size="small">删除</el-button>-->
              </template>
            </el-table-column>
          </el-table>

          <el-dialog title="班级信息" v-model="dialogClassInfoVisible">
            <el-form :model="classInfo">
              <!--              <el-form-item label="学生学号" :label-width="formLabelWidth">-->
              <!--                <el-input v-model="form.studentId" autocomplete="off"></el-input>-->
              <!--              </el-form-item>-->
              <el-form-item label="班级名称" :label-width="formLabelWidth">
                <el-input v-model="classInfo.className" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="班主任教职工号" :label-width="formLabelWidth">
                <el-input v-model="classInfo.teacherInfo.teacherId" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="班主任名称" :label-width="formLabelWidth">
                <el-input v-model="classInfo.teacherInfo.teacherName" autocomplete="off"></el-input>
              </el-form-item>
              <el-table :data="classInfo.studentInfo">
                <el-table-column
                    prop="studentId"
                    label="学生学号"
                    width="180">
                </el-table-column>
                <el-table-column
                    prop="studentName"
                    label="学生姓名"
                    width="180">
                </el-table-column>
                <el-table-column
                    prop="studentSex"
                    label="性别">
                </el-table-column>
                <el-table-column
                    prop="studentBirthday"
                    label="出生日期">
                </el-table-column>
              </el-table>

            </el-form>
            <template #footer>
                    <span class="dialog-footer">
                      <el-button @click="dialogClassInfoVisible = false">关 闭</el-button>
                    </span>
            </template>
          </el-dialog>

          <el-dialog title="添加" v-model="dialogFormVisible" width="50%">
            <el-form :model="classInfo">
              <!--              <el-form-item label="学生学号" :label-width="formLabelWidth">-->
              <!--                <el-input v-model="form.studentId" autocomplete="off"></el-input>-->
              <!--              </el-form-item>-->
              <el-form-item label="班级名称" :label-width="formLabelWidth">
                <el-input v-model="classInfo.className" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="班级地点" :label-width="formLabelWidth">
                <el-input v-model="classInfo.classLocal" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="班主任Id" :label-width="formLabelWidth">
                <el-input @change="searchTeacherName(classInfo.teacherInfo.teacherId)" v-model="classInfo.teacherInfo.teacherId" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="班主任名称" :label-width="formLabelWidth">
                <el-input v-model="classInfo.teacherInfo.teacherName" :disabled="true"></el-input>
              </el-form-item>
            </el-form>
            <template #footer>
                    <span class="dialog-footer">
                      <el-button @click="dialogFormVisible = false">取 消</el-button>
                      <el-button type="primary" @click="pushEditInfo(doMethod)">确 定</el-button>
                    </span>
            </template>
          </el-dialog>

          <el-button class="el-button" size="medium" @click="addInfo">新增</el-button>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import Head from "@/components/Head";
import Aside from "@/components/Aside";
export default {
  components: {
    Head,
    Aside
  },
  name: "Class",
  created() {
    this.getClassInfoList()
  },
  data() {
    return {
      tableData : [],
      dialogClassInfoVisible : false,
      dialogFormVisible : false,
      doMethod : '',
      classInfo : {
        classId: '',
        className : '',
        classLocal : '',
        teacherInfo : {
          teacherId : '',
          teacherName : ''
        },
        studentInfo : []
      },
      formLabelWidth: '120px'

    }
  },
  methods: {
    getClassInfoList() {
      const _this = this
      _this.axios.get("/class/list").then(res => {
        console.log(res.data.data)
        _this.tableData = res.data.data
      })
    },
    showClassInfo(row) {
      console.log(row)
      const _this = this
      _this.axios.get("/class/getClassInfo/"+row.classId).then(res => {
        console.log(res)
        _this.classInfo.classId = res.data.data.classId
        _this.classInfo.className = res.data.data.className
        _this.classInfo.classLocal = res.data.data.classLocal
        _this.classInfo.teacherInfo.teacherId = res.data.data.teacher.teacherId
        _this.classInfo.teacherInfo.teacherName = res.data.data.teacher.teacherName
        _this.classInfo.studentInfo = res.data.data.students
        _this.dialogClassInfoVisible = true
      })
    },
  addInfo() {
    this.classInfo.classId = ''
    this.classInfo.className = ''
    this.classInfo.classLocal = ''
    this.classInfo.teacherInfo.teacherId = ''
    this.classInfo.teacherInfo.teacherName = ''
    this.dialogFormVisible = true
    this.doMethod = 'add'
  },
    pushEditInfo(action) {
      const _this = this;
      const _data = {
        className : this.classInfo.className,
        classLocal : this.classInfo.classLocal,
        teacherId : this.classInfo.teacherInfo.teacherId
      }
      this.axios.post("/class/" + action, _data).then(res => {
        console.log(res)
        _this.dialogFormVisible = false
        _this.getClassInfoList()
      })
    },
    searchTeacherName(id){
      const _this = this
      _this.axios.get("/teacher/search/"+id).then(res => {
        _this.classInfo.teacherInfo.teacherName = res.data.data.teacherName
      }).catch(()=> {
        _this.classInfo.teacherInfo.teacherName = ''
      })
    }
  }
}
</script>

<style scoped>

</style>
